package com.atguigu.webadmin.controller.apartment;


import com.atguigu.common.result.Result;
import com.atguigu.model.pojo.ApartmentInfo;
import com.atguigu.model.enums.ReleaseStatus;
import com.atguigu.webadmin.service.ApartmentInfoService;
import com.atguigu.webadmin.service.CityInfoService;
import com.atguigu.webadmin.service.DistrictInfoService;
import com.atguigu.webadmin.service.ProvinceInfoService;
import com.atguigu.webadmin.vo.apartment.ApartmentDetailVo;
import com.atguigu.webadmin.vo.apartment.ApartmentItemVo;
import com.atguigu.webadmin.vo.apartment.ApartmentQueryVo;
import com.atguigu.webadmin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 梅超凡
 * @Date 2024/11/13~10:31
 * @Description 公寓信息管理控制类
*/
@Tag(name = "公寓信息管理")
@RestController
@RequestMapping("/admin/apartment")
@Slf4j
@CrossOrigin
public class ApartmentController {


    @Autowired
    private ApartmentInfoService apartmentInfoService;


    @Autowired
    private ProvinceInfoService provinceInfoService;

    @Autowired
    private CityInfoService cityInfoService;

    @Autowired
    private DistrictInfoService districtInfoService;
    @Operation(summary = "保存或更新公寓信息")
    @Parameters({
            @Parameter(name = "apartmentSubmitVo",description = "公寓信息vo对象",in = ParameterIn.DEFAULT)
    })
    @PostMapping("saveOrUpdate")
    public Result<Object> saveOrUpdate(@RequestBody @Validated ApartmentSubmitVo apartmentSubmitVo) {
        //未知原因：前端传参不全
        //解决方法，后端根据已有数据进行查询补全
        log.info("{}",apartmentSubmitVo);
        //补全省市区信息
        apartmentSubmitVo.setProvinceName(provinceInfoService.selectProvinceNameById(apartmentSubmitVo.getProvinceId()));
        apartmentSubmitVo.setCityName(cityInfoService.selectCityNameById(apartmentSubmitVo.getCityId()));
        apartmentSubmitVo.setDistrictName(districtInfoService.selectDistrictNameById(apartmentSubmitVo.getDistrictId()));
        //保存或更新公寓信息
        apartmentInfoService.saveOrUpdateApartment(apartmentSubmitVo);
        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询公寓列表")
    @Parameters({
            @Parameter(name = "current",description = "当前页码",in = ParameterIn.DEFAULT),
            @Parameter(name = "size",description = "分页查询数量",in = ParameterIn.DEFAULT),
            @Parameter(name = "queryVo",description = "公寓列表vo对象",in = ParameterIn.DEFAULT)
    })
    @GetMapping("pageItem")
    public Result<IPage<ApartmentItemVo>> pageItem(@RequestParam long current, @RequestParam long size, ApartmentQueryVo queryVo) {
        //根据条件查询分页信息
        IPage<ApartmentItemVo> page = new Page<>(current,size);
        IPage<ApartmentItemVo> list = apartmentInfoService.pageItemApartmentItemByQuery(page,queryVo);
        return Result.ok(list);
    }

    @Operation(summary = "根据ID获取公寓详细信息")
    @Parameters({
            @Parameter(name = "id",description = "公寓id",in = ParameterIn.DEFAULT)
    })
    @GetMapping("getDetailById")
    public Result<ApartmentDetailVo> getApartmentDetailById(@RequestParam Long id) {
        //根据id查询公寓详细信息
        ApartmentDetailVo apartmentInfo = apartmentInfoService.getApartmentDetailById(id);
        return Result.ok(apartmentInfo);
    }

    @Operation(summary = "根据id删除公寓信息")
    @Parameters({
            @Parameter(name = "id",description = "公寓id",in = ParameterIn.DEFAULT)
    })
    @DeleteMapping("removeById")
    public Result<Object> removeApartmentById(@RequestParam Long id) {
        //根据id删除公寓信息
        apartmentInfoService.removeApartmentById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id修改公寓发布状态")
    @Parameters({
            @Parameter(name = "id",description = "公寓id",in = ParameterIn.QUERY),
            @Parameter(name = "status",description = "公寓状态",in = ParameterIn.QUERY)
    })
    @PostMapping("updateReleaseStatusById")
    public Result<Object> updateReleaseStatusById(@RequestParam Long id, @RequestParam ReleaseStatus status) {
        //创建Lambda查询包装类
        LambdaUpdateWrapper<ApartmentInfo> updateWrapper = new LambdaUpdateWrapper<>();
        //指定查询条件为公寓id
        updateWrapper.eq(ApartmentInfo::getId, id);
        //设置要修改的属性
        updateWrapper.set(ApartmentInfo::getIsRelease, status);
        //执行修改
        boolean update = apartmentInfoService.update(updateWrapper);
        //返回修改结果
        return Result.ok(update);
    }

    @Operation(summary = "根据区县id查询公寓信息列表")
    @Parameters({
            @Parameter(name = "id",description = "区县id",in = ParameterIn.QUERY)
    })
    @GetMapping("listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id) {
        //根据区县id查询公寓信息列表
        LambdaQueryWrapper<ApartmentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //指定查询条件
        lambdaQueryWrapper.eq(id != null,ApartmentInfo::getDistrictId,id);
        //执行查询
        List<ApartmentInfo> list = apartmentInfoService.list(lambdaQueryWrapper);
        //打印日志看看查询结果
        log.info("查询结果为:{}",list);
        //返回结果
        return Result.ok(list);
    }
}














